<?php
declare (strict_types=1);

namespace app\model\admin;

use app\common\model\BaseModel;

class Member extends BaseModel
{
    protected $pk = 'member_id';

    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * @param $value
     * @return mixed|string
     * 所属助教
     */
    protected function getAssistantNameAttr($value)
    {
        return Assistant::where('member_id', $value)->value('nickname') ?: '';
    }

    /**
     * 获得列表
     * @param bool $where
     * @param string $fields
     * @param string|null $order
     * @param int $page
     * @param int $pageSize
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($where = true, $fields = '*', $order = null, $page = 1, $pageSize = 20)
    {
        $data = [
            'count' => 0,
            'list' => []
        ];
        $count = $this->where($where)->count();
        if (empty($count)) {
            return $this->returnRight($data);
        }
        $order = is_null($order) ? $this->pk . ' desc' : $order;
        $list = $this->where($where)
            ->field($fields)
            ->order($order)
            ->page($page, $pageSize)
            ->select();
        $data['count'] = $count;
        $data['list'] = $list;
        return $this->returnRight($data);
    }

    /**
     * @param $data
     * 开通助教
     */
    public function openAssistant($data)
    {
        //绑定
        if ($data['type'] == 1) {
            try {
                //查询用户是否绑定助教
                $where['status'] = 1;
                $where['delete_time'] = 0;
                $where['member_id'] = $data['member_id'];
                $count = Assistant::where($where)->count();
                if (!empty($count)) {
                    return $this->returnError(-1, [], '用户已绑定助教');
                }
                $this->startTrans();
                //更新助教表
                Assistant::update(['member_id' => $data['member_id']], ['id' => $data['assistant_id']]);
                Member::update(['identify' => 2, 'role' => 1], ['member_id' => $data['member_id']]);
                $this->commit();
                return $this->returnSucc();
            } catch (\Exception $ex) {
                $this->rollback();
                return $this->returnException($ex);
            }
        } else {
            //查询用户是否绑定助教
            $where['status'] = 1;
            $where['delete_time'] = 0;
            $where['member_id'] = $data['member_id'];
            $count = Assistant::where($where)->count();
            if (!empty($count)) {
                return $this->returnError(-1, [], '用户已绑定助教');
            }
            try {
                $this->startTrans();
                //新增助教
                $list = [
                    'avatar' => $data['avatar'],
                    'nickname' => $data['nickname'],
                    'profile' => $data['profile'],
                    'sign' => $data['sign'],
                    'member_id' => $data['member_id'],
                    'store_id' => $data['store_id'],
                    'grade_id' => $data['grade_id'],
                    'star' => $data['star'],
                    'is_recommend' => $data['is_recommend']
                ];
                Assistant::create($list);
                Member::update(['identify' => 2, 'role' => 1], ['member_id' => $data['member_id']]);
                $this->commit();
                return $this->returnSucc();
            } catch (\Exception $ex) {
                return $this->returnException($ex);
            }
        }
    }
}
